﻿using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace 后整理管理系统.自定义类
{
    public class GetIDHelper : IGetID
    {
        public string GetDanjumingxiDocNum(DataTable table)
        {
            string idstring = string.Empty;
            if (table.Columns.Contains("danhao"))
            {
                foreach (DataRow dr in table.Rows)
                {
                    idstring +="'"+ dr["danhao"].ToString() + "',";
                }
            }
            if (string.IsNullOrEmpty(idstring))
            {
                return idstring;
            }
            else
            {
                return idstring.Substring(0, idstring.Length - 1);
            }
        }
        public List<DanjuTable > GetDanjuTablesFromDocNums(string danhaos)
        {
            return Connect.dbcontext.Ado.SqlQuery<DanjuTable>($"select * from danjutable where dh in ({danhaos})").ToList();
        }
        public DataTable  GetTablesFromDocNums(string danhaos)
        {
            var dt=Connect.dbcontext.Ado.GetDataTable ($"select *,(select top(1) mashu from danjumingxitable where danjumingxitable.danhao=dh) as mashu,(select top(1) pingming from danjumingxitable where danjumingxitable.danhao=dh) as pingming ,(select top(1) hanshuidanjia from danjumingxitable where danjumingxitable.danhao=dh) as hangshuidanjia,(select top(1) danwei from danjumingxitable where danjumingxitable.danhao=dh) as danwei from danjutable where dh in ({danhaos})");
            foreach (DataRow dr in dt.Rows) 
            {
                if (dr["danwei"].ToString ()=="码")
                {
                    dr["je"] = dr["hangshuidanjia"].ObjToDecimal() * dr["Toupimishu"].ObjToDecimal() *(decimal) 0.9144;
                }
            }
            return dt;
        }

        public string GetDanjumingxiID(DataTable table)
        {
            string idstring = string.Empty;
           if(table.Columns.Contains("ID1"))
            {
                foreach(DataRow dr in table.Rows)
                {
                    idstring += dr["ID1"].ToString() + ",";
                }
            }
           if(string.IsNullOrEmpty (idstring))
            {
                return idstring;
            }
           else
            {
                return idstring.Substring (0,idstring.Length - 1);
            }
        }
        public List<danjumingxitable> GetSelectDanjumingxi(DataTable table)
        {
            var idstring = GetDanjumingxiID(table);
            return Connect.dbcontext.Ado.SqlQuery<danjumingxitable>($"select * from danjumingxitable where id in ({idstring})");
        }
    }
}
